package com.sz.sensor.module.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.sz.framework.base.BaseActivity;
import com.sz.framework.utils.ResourceUtil;
import com.sz.framework.widget.ClearEditText;
import com.sz.sensor.R;
import com.sz.sensor.module.main.presenter.LoginPresenter;
import com.sz.sensor.module.main.view.LoginView;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginView {
    @BindView(R.id.riv_header)
    RoundedImageView rivHeader;
    @BindView(R.id.tv_country)
    TextView tvCountry;
    @BindView(R.id.cet_mobile)
    ClearEditText cetMobile;
    @BindView(R.id.cet_psd)
    ClearEditText cetPsd;
    @BindView(R.id.iv_psd_status)
    ImageView ivPsdStatus;

    LoginPresenter presenter;

    @Override
    public int generateLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        BarUtils.setStatusBarColor(this, ResourceUtil.getColor(getContext(),
                R.color.c_00FFFFFF), 0, true);
        presenter = new LoginPresenter();
        presenter.setViewer(this);
    }

    @Override
    public void login() {
        String mobile = cetMobile.getText().toString().trim();
        if(TextUtils.isEmpty(mobile)){
            showToastMessage(R.string.input_phone);
            return;
        }

        String psd = cetPsd.getText().toString().trim();
        if(TextUtils.isEmpty(psd)){
            showToastMessage(R.string.input_psd);
            return;
        }

        presenter.login("86",mobile,psd);
    }

    @Override
    public void onLogin() {
        ActivityUtils.startActivity(MainActivity.class);
        finish();
    }

    @OnClick({R.id.tv_country, R.id.iv_psd_status, R.id.tv_login, R.id.tv_register, R.id.tv_forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_country:
                showToastMessage("+86");
                break;
            case R.id.iv_psd_status:
                ivPsdStatus.setSelected(!ivPsdStatus.isSelected());
                if(ivPsdStatus.isSelected()){
                    // 显示
                    cetPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    // 隐藏
                    cetPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                cetPsd.setSelection(cetPsd.getText().length());
                break;
            case R.id.tv_login:
                login();
                break;
            case R.id.tv_register:
                showToastMessage("快速注册");
                break;
            case R.id.tv_forget:
                showToastMessage("忘记密码");
                break;
        }
    }
}
